//
//  BRSAppDelegate.m
//  Pint
//
//  Created by xiect on 13-10-22.
//  Copyright (c) 2013年 brains. All rights reserved.
//

#import "BRSAppDelegate.h"

#import "const.h"
#import "BRSViewController.h"
#import "BRSEluaControllerViewController.h"
#import "BRSDatabaseHelp.h"

@implementation BRSAppDelegate
static UIAlertView *sAlertView = nil;

#pragma mark-
#pragma mark エラー情報表示について
- (void)alert:(NSString*)title message:(NSString*)message {
    if (sAlertView) return;
    sAlertView = [[UIAlertView alloc] initWithTitle:title
                                        message:message
                                       delegate:self
                              cancelButtonTitle:@"確認"
                              otherButtonTitles:nil];
    [sAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    sAlertView = nil;
}

#pragma mark-
#pragma mark アプリ　リサイクルメソード
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
//    self.viewController = [[BRSViewController alloc] initWithNibName:@"BRSViewController" bundle:nil];
//    self.viewController.title = @"title";

    // just for test DB
//    BRSDatabaseHelp *databaseHelp = [BRSDatabaseHelp sharedDatabase];
//    FMDatabase* db = [databaseHelp connect2Database];
//    if(databaseHelp){
//        [databaseHelp open];
//        FMResultSet * rs = [db executeQuery:@"select area from AreaMaster"];
//        while ([rs next]) {
//            NSString *name = [rs stringForColumn:@"area"];
//            NSLog(@"name--->%@",name);
//        }
//        [rs close];
//        [databaseHelp close];
//    }
    
    UIViewController *controller = [[BRSEluaControllerViewController alloc] init];
    self.navController = [[UINavigationController alloc] initWithRootViewController:controller];
 

    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



// exit app
- (void)exitApplication {
    [UIView beginAnimations:@"exitApplication" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    
    // [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.view.window cache:NO];
    
    [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
    
    //self.view.window.bounds = CGRectMake(0, 0, 0, 0);
    
    self.window.bounds = CGRectMake(0, 0, 0, 0);
    [UIView commitAnimations];
}

- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if ([animationID compare:@"exitApplication"] == 0) {
        exit(0);
    }
}
@end
